package com.ling.pl.security.service;

import com.ling.pl.core.commons.support.DaoPage;
import com.ling.pl.core.commons.utils.BatchUtils;
import com.ling.pl.core.commons.utils.reflect.ReflectUtils;
import com.ling.pl.core.dao.IDao;
import com.ling.pl.core.service.impl.BaseService;
import com.ling.pl.security.dao.DefaultRoleDao;
import com.ling.pl.security.model.DefaultRole;
import com.ling.pl.security.model.DefaultRoleVO;

import javax.annotation.Resource;
import java.util.*;

/**
 * @author bo.wang
 */
public class BaseDefaultRoleService extends BaseService {

	public static final String BEAN_ID = "defaultRoleService";

	@Resource
	DefaultRoleDao defaultRoleDao;

	public IDao getDao() {
		return defaultRoleDao;
	}

	public List<DefaultRoleVO> convertDefaultRoleToVO(List<DefaultRole> models) {
		List<DefaultRoleVO> result = new ArrayList<DefaultRoleVO>();
		for (DefaultRole initiation : models) {
			DefaultRoleVO inparam = convertDefaultRoleToVO(initiation);
			result.add(inparam);
		}
		return result;
	}

	public DefaultRoleVO convertDefaultRoleToVO(DefaultRole model) {
		DefaultRoleVO inparam = new DefaultRoleVO();
		ReflectUtils.copyProperties(model, inparam);
		return inparam;
	}

	public DefaultRole convertDefaultRoleVOTo(DefaultRoleVO vo) {
		DefaultRole entity = new DefaultRole();
		ReflectUtils.copyProperties(vo, entity);
		return entity;
	}

	public List<DefaultRole> convertDefaultRoleVOTo(List<DefaultRoleVO> vos) {
		List<DefaultRole> result = new ArrayList<DefaultRole>();
		for (DefaultRoleVO vo : vos) {
			DefaultRole entity = convertDefaultRoleVOTo(vo);
			result.add(entity);
		}
		return result;
	}

	public DaoPage findDefaultRoleVOByParams(Map params, Integer pageIndex,
											 Integer pageSize) {
		if (params == null) {
			params = new HashMap();
		}
		DaoPage daoPage = defaultRoleDao.findDefaultRoleByParams(params, pageIndex, pageSize);
		daoPage.setResult(convertDefaultRoleToVO((List<DefaultRole>) daoPage.getResult()));
		return daoPage;
	}

	public List<DefaultRoleVO> findDefaultRoleVOByParams(Map params) {
		if (params == null) {
			params = new HashMap();
		}
		List<DefaultRole> temp = defaultRoleDao.findDefaultRoleByParams(params);
		return convertDefaultRoleToVO(temp);
	}

    public DaoPage findDefaultRoleByParams(Map params, Integer pageIndex,
										   Integer pageSize) {
		if (params == null) {
			params = new HashMap();
        }
		DaoPage daoPage = defaultRoleDao.findDefaultRoleByParams(params, pageIndex, pageSize);
        return daoPage;
    }

    public List<DefaultRole> findDefaultRoleByParams(Map params) {
		if (params == null) {
			params = new HashMap();
        }
		List<DefaultRole> temp = defaultRoleDao.findDefaultRoleByParams(params);
        return temp;
    }


	public void saveDefaultRoleDataListsMap(Map<String, List<DefaultRoleVO>> dataListsMap) {
		Collection<DefaultRoleVO> deleteList = BatchUtils.to(DefaultRoleVO.class).getDeleteEntities(dataListsMap);
		Collection<DefaultRoleVO> insertList = BatchUtils.to(DefaultRoleVO.class).getInsertEntities(dataListsMap);
		Collection<DefaultRoleVO> updateList = BatchUtils.to(DefaultRoleVO.class).getModifiedEntities(dataListsMap);
		if (updateList != null && updateList.size() > 0) {
			for (DefaultRoleVO vo : updateList) {
				DefaultRole entity = convertDefaultRoleVOTo(vo);
				defaultRoleDao.update(entity);
			}
//			defaultRoleDao.updateAll(updateList);
		}
		if (insertList != null && insertList.size() > 0) {
			for (DefaultRoleVO vo : insertList) {
				DefaultRole entity = convertDefaultRoleVOTo(vo);
				defaultRoleDao.save(entity);
				vo.setId(entity.getId());
			}
//			defaultRoleDao.saveAll(insertList);
		}
		if (deleteList != null && deleteList.size() > 0) {
			for (DefaultRoleVO vo : deleteList) {
				DefaultRole entity = new DefaultRole();
				entity.setId(vo.getId());
				defaultRoleDao.remove(entity);
			}
//			defaultRoleDao.removeAll(deleteList);
		}

	}
}

